<template>
	<view>
		<view class="search-box">
			<my-search bg-color="#007AFF" :border-radius="16" @click="goSearch"></my-search>
		</view>
		<!-- 轮播图部分 -->
		<swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
			<swiper-item v-for="(item, i) in swiperList" :key="i">
				<navigator class="swiper-item" :url="'/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id">
					<!-- 动态绑定图片的 src 属性 -->
					<image :src="item.image_src"></image>
				</navigator>
			</swiper-item>

		</swiper>
		<!-- 导航分类列表部分 -->
		<view class="nav-list">
			<view class="nav-img" v-for="(item,index) in cateList" :key="index" @click="navClickHandler(item)">
				<image :src="item.image_src" class="nav-img"></image>
			</view>
		</view>
		<!-- //楼层数据 -->
		<view class="floor-list">
			<view class="floor-item" v-for="(item, index) in floorList" :key="index">
				<!-- 楼层标题 -->
				<image :src="item.floor_title.image_src" class="floor-title"></image>

				<view class="floor-img-box">
					<!-- 左侧大图片的盒子 -->
					<view class="left-img-box">
						<navigator :url="item.product_list[0].navigator_url">
							<image :src="item.product_list[0].image_src"
								:style="{width: item.product_list[0].image_width + 'rpx'}" mode="widthFix"></image>
						</navigator>
					</view>
					<!-- 右侧 4 个小图片的盒子 -->
					<view class="right-img-box">
						<view class="right-img-item" v-for="(val,idx ) in item.product_list" :key="idx"
							v-if="idx !== 0">
							<navigator :url="val.navigator_url">
								<image :src="val.image_src" mode="widthFix" :style="{width: val.image_width + 'rpx'}">
								</image>
							</navigator>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 定义轮播图数据
				swiperList: [],
				cateList: [],
				// 楼层数据
				floorList: []
			};
		},
		onLoad() {
			// 调用获取轮播数据
			this.getSwiperList()
			this.getCateList()
			this.getFloorList()
		},
		methods: {
			// 请求轮播图数据
			async getSwiperList() {
				// 发送请求
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/swiperdata')
				// 错误处理
				if (res.meta.status !== 200) return uni.$showMsg()
				// 成功赋值
				this.swiperList = res.message
			},
			//请求分类列表数据
			async getCateList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/catitems')
				if (res.meta.status !== 200) return uni.$showMsg()
				this.cateList = res.message
			},
			navClickHandler(val) {
				if (val.name === '分类') {
					uni.switchTab({
						url: '/pages/cate/cate'
					})
				}
			},
			async getFloorList() {
				const {
					data: res
				} = await uni.$http.get('/api/public/v1/home/floordata')
				if (res.meta.status !== 200) return uni.$showMsg()
				res.message.forEach(val => {
					let product_list = val.product_list
					product_list = product_list.map(item => {
						item.navigator_url = item.navigator_url.replace('/pages', '/subpkg/goods_list')
						return item


					})
				})
				this.floorList = res.message




			},
			goSearch(){
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}

		}

	}
</script>

<style lang="scss">
	.search-box {
	  position: sticky;
	  top: 0;
	  z-index: 999;
	}
	swiper {
		height: 330rpx;

		.swiper-item,
		image {
			width: 100%;
			height: 100%;
		}
	}

	.nav-list {
		display: flex;
		justify-content: space-around;
		margin: 15px 0;

		.nav-img {
			width: 128rpx;
			height: 140rpx;
		}
	}

	.floor-title {
		height: 60rpx;
		width: 100%;
		display: flex;
	}

	.right-img-box {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around;
	}

	.floor-img-box {
		display: flex;
		padding-left: 10rpx;
	}
</style>
